package homework.scp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class FtpServer {

	public static void main(String[] args) throws Exception {
		OutputStream fout;
		BufferedReader reader;
		ServerSocket server = new ServerSocket(8001);
		Socket socket = server.accept();
		OutputStream out = socket.getOutputStream();
		InputStream in = socket.getInputStream();
		reader = new BufferedReader(new InputStreamReader(in));
		
		out.write("ready\n".getBytes());

		String sourceFileName = reader.readLine();
		String s = reader.readLine();
		
		String filename = s.substring(s.indexOf("/"));
		if (filename.equals("/")) { // 保留原文件名
			fout = new FileOutputStream("" + sourceFileName);
		} else { // 以现在文件名命名
			fout = new FileOutputStream("" + filename);
		}

		byte[] buf=new byte[4];
		int len=0;
		while((len=in.read(buf))!=-1){
			fout.write(buf,0,len);
		}

		
		reader.close();
		out.close();
		fout.close();
		socket.close();

	}

}
